<?php

use Biz\model\wiki\MGoodsAlbum;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\ActionLog;
use Doraemon\tools\BlockType;
use Doraemon\tools\Hhzurl;

/**
 * 6.2版本之前 清单专辑
 * @deprecated
 */
class GoodsAlbumListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const PAGE_SIZE = 20;

    public function main()
    {
        $page = $this->getRequest()->getQuery('page', 1);
        $page = $page ?? 1;
        $albumList = $this->getAlbumList($page);
        $this->setSuccess($albumList);
    }

    private function getAlbumList($page)
    {
        $result = [];
        $res = MGoodsAlbum::apiAllList($view = in_array($this, [2537919]), $page, self::PAGE_SIZE);
        $total = $res['total'];
        $wikiIds = [];
        $list = [];
        foreach ($res['list'] as $row) {
            $wiki = $row['wiki'] ? explode(',', $row['wiki']) : [];
            $wikiId = $wiki ? $wiki[0] : 0;
            $row['wiki_id'] = $wikiId;
            $wikiIds[] = $wikiId;
            $list[] = $row;
        }
        $wikiList = $wikiIds ? clsWiki::getWikiBaseInfos($wikiIds) : [];
        $index = 1;
        foreach ($list as $item) {
            $wikiId = $item['wiki_id'];
            $coverImg = $wikiList[$wikiId]['cover_img'] ?? '';
            $middleLink = $wikiId ? Hhzurl::H5WikiMiddleList($wikiId, ActionLog::GOODTINGS_ALBUM) . "&goods_album=1&album_id=" . $item['id'] : '';
            if ($coverImg) {
                $result[] = [
                    'type' => BlockType::TYPE_GOODS_ALBUM,
                    'album_info' => [
                        'title' => $item['title'],
                        'cover_img' => $coverImg,
                        'middle_link' => \Biz\tools\helper\Common::formatFullScreenLink($middleLink),
                    ],
                    'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_TYPE_ALBUM_LIST_PAGE_ALBUM_LIST, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_GOODS_ALBUM,
                        basicStatSign::EXTEND_KEY_OBJ_ID => $item['id'],
                        basicStatSign::EXTEND_KEY_INDEX => $index
                    ]),
                ];
                $index++;
            } else {
                \Doraemon\tools\Log::debug('未查询到wiki到图片#wiki_id:' . $wikiId);
            }
        }
        return [
            'list' => $result,
            'is_over' => ($page * self::PAGE_SIZE >= $total) ? 1 : 0
        ];
    }

}